<template>
  <div class="user-editor">
    <Form
      :model="formInfo"
      ref="formRole"
      class="ivu-form-no-margin-bottom"
      :disabled="false"
      :label-width="120"
    >
      <Row :gutter="20" class="code-row-bg">
        <Col span="24" style="border: none;">
          <Form-item prop="dispatchStartTime" label="停止调度时间从">
            <DatePicker
              type="datetime"
              format="yyyy-MM-dd HH:mm"
              :transfer="true"
              :editable="false"
              :clearable="true"
              v-model="formInfo.dispatchStartTime"
              placement="bottom-end"
              style="width: 60%;"
              size="small"
              placeholder="请选择停止调度起始时间">
            </DatePicker>
          </Form-item>
        </Col>
        <Col span="24" style="border: none;">
          <Form-item prop="dispatchEndTime" label="停止调度时间至">
            <DatePicker
              type="datetime"
              format="yyyy-MM-dd HH:mm"
              :transfer="true"
              :editable="false"
              :clearable="true"
              v-model="formInfo.dispatchEndTime"
              placement="bottom-end"
              style="width: 60%;"
              size="small"
              placeholder="请选择停止调度起始时间">
            </DatePicker>
          </Form-item>
        </Col>
      </Row>
    </Form>
  </div>
</template>

<script>
import { mixin } from "@/views/superviseInfo/mix/mixin";
import { superviseInfoAPI } from "@api/superviseInfo";
export default {
  name: "DispatchConfigEditor",
  mixins: [mixin],
  props: {
    handleType: {
      type: String,
      default: ''
    }
  },
  computed: {},
  data() {
    return {
      formInfo: {
        dispatchStartTime: '',
        dispatchEndTime: ''
      },
      actions: [
        {
          text: "确定",
          handle: () => {
            this.submit()
          }
        },
        {
          text: "取消",
          handle: () => {
            this.$emit('Close')
          }
        },
      ],
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      let title = '新增调度配置'
      this.$emit('SetPageActions', this.actions)
      this.$emit('SetTitle', title)
      this.$emit("SetPageWidth", 550);
      this.$emit("SetPageMinHeight", 150);
    },
    submit() {
      this.$refs.formRole.validate((valid) => {
        if (valid) {
          
          // 起始时间无，结束时间有---不合法输入类型
          if (this.formInfo.dispatchStartTime == '' && this.formInfo.dispatchEndTime) {
            this.$Message.warning({
              content: "当停止调度结束时间有值时停止调度起始时间不能为空",
              duration: 5
            });
            return
          // 起始时间和结束时间都有值的情况, 结束时间必须晚于开始时间
          } else if (this.formInfo.dispatchStartTime && this.formInfo.dispatchEndTime && (this.formInfo.dispatchStartTime.getTime() >= this.formInfo.dispatchEndTime.getTime())){
            this.$Message.warning({
              content: "停止调度结束时间必须晚于停止调度开始时间",
              duration: 5
            });
            return
          }

          let dispatchStartTime = this.formInfo.dispatchStartTime ? this.formInfo.dispatchStartTime.Format('yyyy-MM-dd hh:mm:ss') : ''
          let dispatchEndTime = this.formInfo.dispatchEndTime ? this.formInfo.dispatchEndTime.Format('yyyy-MM-dd hh:mm:ss') : ''
          
          let params = {
            dispatchStartTime,
            dispatchEndTime
          }
          superviseInfoAPI.addDispatchConfig(params).then(res => {
            if (res.data.code === '-1') {
              this.$message.error(res.data.message)
            } else {
              this.$message.success('操作成功')
              this.$emit('Close')
            }
          })
        }
      })
    }
  }
};
</script>

<style scoped>

</style>
